package com.yijiu.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yijiu.wiki.domain.Category;
import com.yijiu.wiki.domain.CategoryExample;
import com.yijiu.wiki.mapper.CategoryMapper;
import com.yijiu.wiki.req.CategoryQueryReq;
import com.yijiu.wiki.req.CategorySaveReq;
import com.yijiu.wiki.resp.CategoryQueryResp;
import com.yijiu.wiki.resp.PageResp;
import com.yijiu.wiki.util.CopyUtil;
import com.yijiu.wiki.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ligaojian
 * @version 1.0
 * @description: TODO
 * @date 2021/10/30 9:47
 */
@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<CategoryQueryResp> all(){

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        //列表复制
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        return list;
    }
    public PageResp<CategoryQueryResp> list(CategoryQueryReq req){

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        //使用pageHelper插件实现分页
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
//        pageInfo.getTotal();
//        pageInfo.getPages();
//        List<CategoryResp> respList = new ArrayList<>();
//        for (Category category : categoryList) {
////            CategoryResp categoryResp = new CategoryResp();
////            BeanUtils.copyProperties(category,categoryResp); //对象的复制
//
//            CategoryResp categoryResp = CopyUtil.copy(category, CategoryResp.class);
//            respList.add(categoryResp);
//        }

        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    /**
    * @description: 编辑电子书中的保存
    * @author ligaojian
    * @date
    * @version 1.0
    */
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req,Category.class);
        if (ObjectUtils.isEmpty(req.getId()) || req.getId()==0){
            //新增,使用雪花算法生成id
//            category.setId(String.valueOf(snowFlake.nextId()));
            category.setId(snowFlake.nextId());

            System.out.println("新增数据的id"+category.getId());
            categoryMapper.insert(category);
        }else {
            System.out.println("1111111111"+req.getId());
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /**
    * @description: 删除电子书
    * @author
    * @date
    * @version 1.0
    */
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
        System.out.println("删除成功！");
    }
}
